package com.care.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.care.entity.Doctor;
import com.care.entity.Knowledge;
import com.care.entity.Page;
import com.care.entity.User;
import com.care.service.AdminServiceImpl;



@Controller
public class AdminController {

	@Resource
	private AdminServiceImpl asi;
	
	//首页
	@RequestMapping("/")
	public String showIndex(){
		return "index";
	}

	//查询用户
	@RequestMapping("/selectuser")
	public String selectCus(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String name = request.getParameter("name");
		User user =  asi.selUser(name);
		request.setAttribute("user", user);
		return "/admin_user_sel_after.jsp";
	}
	
	//查询医生
	@RequestMapping("/selectdoctor")
	public String selectDoc(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String name = request.getParameter("name");
		Doctor doctor =  asi.selDoctor(name);
		request.setAttribute("Doctor", doctor);
		return "/admin_doctor_sel_after.jsp";
	}
	//查询医护知识	
	@RequestMapping("/selectknowledge/{title}")
	public String selectKnowledge(HttpServletRequest request,HttpServletResponse response,@PathVariable String title) throws ServletException, IOException{
		Knowledge knowledge =  asi.selKnowledge(title);
		request.setAttribute("Knowledge", knowledge);
		return "/admin_konwledge_sel_after.jsp";
	}
	
	
	
	//加用户
	@RequestMapping("/adduser")
	public String addUser(HttpServletRequest request){
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String sex = request.getParameter("sex");
		String email = request.getParameter("email");
		if(asi.addUser(name, password, sex, email)){
			return "/admin_user_add_after.jsp";
		}
		return "/admin_user_add_after.jsp";
	}
	//加医生
	@RequestMapping("/adddoctor")
	public String addDoctor(HttpServletRequest request){
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String sex = request.getParameter("sex");
		String information = request.getParameter("information");
		double money = Double.parseDouble(request.getParameter("money"));
		String section = request.getParameter("section");
		String email = request.getParameter("email");
		if(asi.addDoctor(name, password, sex, information, money, section, email)){
			return "selalldoctor";
		}
		return "selalldoctor";
	}
	//加医护知识
	@RequestMapping("/addknowledge")
	public String addKnowledge(HttpServletRequest request){
		String name = request.getParameter("name");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String doctorname = request.getParameter("doctorname");
		if(asi.addKnowledge(title, content, doctorname)){
			return "selectuser";
		}
		return "selectuser";
	}
	
	
	
	//删用户
	@RequestMapping("/deluser/{name}")
	public String delUser(HttpServletRequest request,@PathVariable("name") String name){
		asi.delUser(name);
		return "首页";
	}
	//删医生
	@RequestMapping("/deldoctor/{name}")
	public String delDoctor(HttpServletRequest request,@PathVariable("name") String name){
		asi.delDoctor(name);
		return "首页";
	}
	//删医护知识
	@RequestMapping("/delknowledge/{title}")
	public String delKnowledge(HttpServletRequest request,@PathVariable("title") String title){
		asi.delKnowledge(title);
		return "首页";
	}
	
	
	//改用户
	@RequestMapping("upduser")
	public String updUser(HttpServletRequest request){
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String img = request.getParameter("img");
		String sex = request.getParameter("sex");
		Double money = Double.parseDouble(request.getParameter("money"));
		String email = request.getParameter("email");
		if(asi.updUser(name, password, img, sex, money, email)){
			return "selalluser";
		}return "首页";
	}
	//改医生
	@RequestMapping("upddoctor")
	public String updDoctor(HttpServletRequest request){
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String information = request.getParameter("information");
		String sex = request.getParameter("sex");
		Double money = Double.parseDouble(request.getParameter("money"));
		String section = request.getParameter("section");
		String email = request.getParameter("email");
		if(asi.updDoctor(name, password, sex, information, money, section, email)){
			return "selalldoctor";
		}return "首页";
	}	
	//改医护知识
	@RequestMapping("updknowledge")
	public String updKnowledge(HttpServletRequest request){
		String name = request.getParameter("name");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String doctorname = request.getParameter("doctorname");
		if(asi.updKnowledge(name, title, content, doctorname)){
			return "selallknowledge";
		}return "首页";
	}
	
	
	
	//查询分页用户
	@RequestMapping("selalluser")
	public String sellAllUser(HttpServletRequest request){
		String number = request.getParameter("number");
		Page<User> pageuser = asi.selAllUser(number);
		request.setAttribute("pageuser", pageuser);
		return "admin_user_page.jsp";
	}
	//查询分页医生
	@RequestMapping("selalldoctor")
	public String sellAllDoctor(HttpServletRequest request){
		String number = request.getParameter("number");
		Page<Doctor> pagedoctor = asi.selAllDoctor(number);
		request.setAttribute("pagedoctor", pagedoctor);
		return "admin_doctor_page.jsp";
	}
	//查询分页医生
	@RequestMapping("selallknowledge")
	public String sellAllKnowledge(HttpServletRequest request){
		String number = request.getParameter("number");
		Page<Knowledge> pageknowledge = asi.selAllKnowledge(number);
		request.setAttribute("pageknowledge", pageknowledge);
		return "admin_doctor_page.jsp";
	}
}
